﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

namespace Jy123.Web.Album
{
    public partial class PhotoEditPortrait : System.Web.UI.Page
    {
        private int userID;
        protected void Page_Load(object sender, EventArgs e)
        {
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);

            string stralbumID = Request.QueryString["AlbumID"];
            if (string.IsNullOrEmpty(stralbumID))
            {
                stralbumID = Request.Form["hidAlbumID"].ToString();
            }
            int albumID = Convert.ToInt32(stralbumID);
            string strhidAlbumID = Request.Form["hidAlbumID"];
            string strSavePhotos = Request.Form["savePhotos"];
            string msg = "";
            if (strSavePhotos == "保存编辑")
            {
                //保存照片
                string txtDescriptions = Request.Form["txtDescription"];
                string hidPhotoIDs = Request.Form["hidPhotoID"];
                //int effectRows = new BLL.User.Album().UpdatePhotos(hidPhotoIDs, txtDescriptions);
                //if (effectRows > 0) { msg = "<script>$(function(){$.moonbox.ok('保存所有照片成功!');})</script>"; }
                //保存头像
                string strHidPhoto = Request.Form["rdioSetCover"];
                //string strhidFilePath = Request.Form["hidFilePath"];
                if (!string.IsNullOrEmpty(strHidPhoto))
                {
                    //new BLL.User.Album().AlbumSetCover(Convert.ToInt32(strHidPhoto));
                    Jy123.Model.User_Main mUser = new Jy123.Model.User_Main();
                    Jy123.BLL.User_Main bllUser = new Jy123.BLL.User_Main();
                    mUser = bllUser.GetModel(userID);
                    mUser.Portrait = strHidPhoto;
                    bllUser.Update(mUser);
                    msg = "<script>$(function(){$.moonbox.ok('保存所有照片成功!');})</script>";
                }
            }
            #region 公共部分
            StringBuilder strHtml = new StringBuilder(Cache["user_photoeditportrait"].ToString());//取得主HTML页面
            strHtml = strHtml.Replace("$Header$", Cache["user_header2"].ToString());
            strHtml = strHtml.Replace("$Footer$", Cache["user_footer"].ToString());
            strHtml = strHtml.Replace("$LeftUserCmd$", Cache["user_navigation_down"].ToString());
            strHtml = strHtml.Replace("$LeftUserFriendAdd$", Cache["user_friendadd"].ToString());

            //统计站内信数量
            Jy123.BLL.User.Letter bllLetter = new Jy123.BLL.User.Letter();
            int newLetterCount = bllLetter.CountNewLetter(userID);
            strHtml = strHtml.Replace("$NewMsg$", newLetterCount.ToString());
            #endregion
            //编辑相册部分(读取相册信息及列出所属相片列表)           
            Jy123.Model.User_Album mAlbum = new Jy123.Model.User_Album();
            Jy123.BLL.User_Album bllAlbum = new Jy123.BLL.User_Album();
            mAlbum = bllAlbum.GetModel(albumID);
            strHtml = strHtml.Replace("$ImgCount$", mAlbum.ImagesCount.ToString())
                .Replace("$Title$", mAlbum.Title);
            StringBuilder sbPhotoUplist = new StringBuilder();
            string strPhotoUplist = Cache["user_photoeditportrait_li"].ToString();
            DataTable dtPhotoUplist = new BLL.User.Album().PhotoList(albumID, "ID DESC", 1, int.MaxValue).Tables[0];
            string userPortrait = new BLL.User_Main().GetModel(userID).Portrait;
            for (int i = 0; i < dtPhotoUplist.Rows.Count; i++)
            {
                string str = strPhotoUplist;
                str = str.Replace("$FilePath$", new AppCode.CreateDir().ReDraw(100, 0, dtPhotoUplist.Rows[i]["FilePath"].ToString()))
                    .Replace("$Title$", dtPhotoUplist.Rows[i]["Title"].ToString())
                    .Replace("$Description$", dtPhotoUplist.Rows[i]["Description"].ToString())
                    .Replace("$PhotoID$", dtPhotoUplist.Rows[i]["ID"].ToString());
                //bool chk = (bool)dtPhotoUplist.Rows[i]["isCover"];
                string chk = dtPhotoUplist.Rows[i]["FilePath"].ToString();
                if (chk == userPortrait) { str = str.Replace("$checked$", "checked"); }
                else { str = str.Replace("$checked$", ""); }
                sbPhotoUplist.Append(str);
            }
            strHtml = strHtml.Replace("$PhotoUplist_li$", sbPhotoUplist.ToString())
                .Replace("$AlbumID$", albumID.ToString());
            Response.Write(strHtml.ToString() + msg);
        }
    }
}
